import {
  createStoreBindings
} from "mobx-miniprogram-bindings";

import {
  WorkOrderReviewApi,
} from "../../common/apis/workOrder"
import {
  getElHeightRpx,
} from "../../utils/dom"
import {
  SUB_PAGES
} from '../../config/router.config'
import {
  handleCheckBoxChecked
} from "../../utils/util"
import {
  workOrderReviewIndexFilterStore
} from "../../store/workOrderReviewIndexFilterStore";

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    bodyHeight: 0,
    dataList: [],
    // 显示全选
    showAll: false,
    // 选中的值
    checkedValue: [],

  },
  // 批量通过
  onPassThrough() {
    this.setData({
      showAll: true

    })
  },
  onClose() {
    this.setData({
      showAll: false
    })
  },

  styleInit() {
    const self = this
    getElHeightRpx('#header', (height) => {
      self.setData({
        bodyHeight: app.globalData.viewHeight - height
      })
    })
  },
  dataInit() {
    const self = this

    const {
      buildIdList,
      completeEndTime,
      completeStartTime,
      deptIdList,
      faultTypeList,
      keywords,
      makeDeptIdList,
      makeTeamIdList,
      makeUserIdList,
      repairEndTime,
      repairStartTime
    } = this.data
    const parmas = {
      buildIdList,
      completeEndTime,
      completeStartTime,
      deptIdList,
      faultTypeList,
      keywords,
      makeDeptIdList,
      makeTeamIdList,
      makeUserIdList,
      repairEndTime,
      repairStartTime
    }
    WorkOrderReviewApi.getList(parmas, (res) => {
      self.setData({
        dataList: res.data.rows
      })
    })

  },
  // 

  // 点击复选框
  changeCheckBox: function (event) {
    const {
      currentTarget,
      detail
    } = event
    const val = handleCheckBoxChecked(currentTarget.dataset.value, detail.checked, this.data.checkedValue)
    console.log(val, '---val');
    this.setData({
      checkedValue: val
    })
  },
  // 全选
  onAll() {
    this.setData({
      checkedValue: this.data.dataList.map((item => item.id))
    })
  },
  // 取消全选
  onClose() {
    this.setData({
      checkedValue: [],
      showAll: false
    })
  },
  // 通过
  onPass() {
    const params = {
      orderNums: this.data.checkedValue.join(',')
    }
    const self = this
    WorkOrderReviewApi.pass(params, ({
      code
    }) => {
      if (code === 200) {
        self.dataInit()
      }
    })
  },
  // 驳回
  onReject(e) {
    wx.navigateTo({
      url: `${SUB_PAGES.WorkOrder.WORK_ORDER_REVIEW_INDEX_REJECT.router}?orderNum=${e.currentTarget.dataset.orderNum}`
    })
  },

  // 点击卡片
  onCard({
    detail
  }) {
    console.log(detail);
    const {
      id
    } = detail.value
    // wx.navigateTo({
    //   url: `${SUB_PAGES.WorkOrder.WORK_ORDER_DETAIL.router}?orderId=${id}`
    // })
  },
  // 点击筛选
  onFilter() {
    wx.navigateTo({
      url: `${SUB_PAGES.WorkOrder.WORK_ORDER_REVIEW_INDEX_FILTER.router}`
    })
  },

  // 滚动底部触发
  onScrollBottom() {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.storeBindings = createStoreBindings(this, {
      store: workOrderReviewIndexFilterStore,
      fields: ['buildIdList', 'completeEndTime', 'completeStartTime', 'deptIdList', 'faultTypeList', 'keywords', 'makeDeptIdList', 'makeTeamIdList', 'makeUserIdList', 'repairEndTime', 'repairStartTime', ],
    });
    this.storeBindings.updateStoreBindings()
    this.styleInit()
    this.dataInit()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.storeBindings.destroyStoreBindings()

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})